clearvars;

% =========================================================================
% 从血管图像中随机提取一行或几行形成点状结构
% =========================================================================

% create the computational grid
Nx = 534;  % number of grid points in the x (row) direction
Ny = 268;  % number of grid points in the y (column) direction

% create initial pressure distribution using makeDisc

num_images = 1000; % 要生成的图像数量

for img_idx = 1 : num_images

    % create initial pressure distribution
    source.p0 = zeros(Nx, Ny); % 初始化压力分布为零矩阵

    source_path = './STARE_Dataset/training';

    %随机读取一个文件，随机选择1行或2行，扩充（插值）到268，插入到随机的行中
    % 随机读取一个文件
    files = dir(fullfile(source_path, '*.gif')); % 获取文件夹中所有png文件
    disp(randi([1, numel(files)], 1, 1));
    random_file = files(randi([1, numel(files)], 1, 1)).name; % 随机选择一个文件
    img = imread(fullfile(source_path, random_file)); % 读取图像文件

    % 获取图像的尺寸（高度和宽度）
    [img_height, img_width] = size(img); % 获取图像的高度和宽度

    % 随机选择1行或2行
    num_rows_to_select = randi([1, 2]); % 随机选择1行或2行
    for row_idx = 1 : num_rows_to_select
        start_row = randi([50, img_height - num_rows_to_select + 1], 1, 1); % 随机选择起始行
        selected_rows = img(start_row:start_row + num_rows_to_select - 1, :); % 选择行

        % 扩充（插值）到268列
        resized_rows = imresize(selected_rows, [num_rows_to_select, Ny]); % 调整大小到268列

        % 插入到随机的行中
        insert_start_row = randi(Nx - num_rows_to_select + 1); % 随机选择插入的起始行
        source.p0(insert_start_row:insert_start_row + num_rows_to_select - 1, :) = resized_rows; % 插入到source.p0
    end

    % smooth the initial pressure distribution and restore the magnitude
    source.p0 = smooth(source.p0, true);

    % =========================================================================
    % SAVE RESULTS，需要保存ground-truth图像
    % =========================================================================
    % 定义保存路径和文件名
    filename = sprintf('./VESSEL_POINTS_Dataset_II/points_initial_pressure_%04d.png', img_idx);
    
    % 将 source.p0 的数据范围缩放到 [0, 255] 以便于保存为图像
    scaled_p0 = mat2gray(source.p0);
    
    % 使用 imwrite 函数保存图像
    imwrite(uint8(scaled_p0 * 255), filename);
    % =========================================================================
    % VISUALISATION
    % =========================================================================
    % plot the initial pressure and sensor distribution
%     figure;
%     imagesc(scaled_p0);
%     colormap(getColorMap);    
end

